// pages/topicDetail/topicDetail.js
const app = getApp()
//const user = JSON.parse(wx.getStorageSync("user"));
const base_url = app.globalData.base_url;
var util = require('../../utils/util.js');
var WxParse = require('../../common/wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAct:'0',
    topic_id:'',
    page:{
      p:0,
      psize:10
    },
    topicObj:{},
    postList:[]//当前话题帖子
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '话题详情'
    })
    this.setData({
      topic_id:options.id
    })
    this.init()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  nav:function(e){
    this.setData({
      isAct:e.currentTarget.dataset.idx
    })
  },
  onReachBottom() {
    wx.showLoading({
      title: '加载中',
      success: () => {
        this.setData({
          'page.p': this.data.page.p + 1
        })
        this.init()

      }
    })
  },
  onPullDownRefresh() {
    let refresh = true
    this.setData({
      'page.p': 0
    })
    this.init(refresh)
  },
  init(refresh = false){
    var that = this;
    wx.showLoading({
      title: '加载中',
      success:()=>{
        wx.request({
          url: base_url + '/Topic/detail',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'id': this.data.topic_id,
            'p':this.data.page.p,
            'psize':this.data.page.psize
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            wx.hideLoading();
            if (res.data.success == '1') {
              if (res.data.data.post_list.length == 0){
                wx.showToast({
                  title: '暂无数据',
                  icon: 'none',
                  duration: 2000
                })
              }else{
                let arr = refresh ? res.data.data.post_list : this.data.postList.concat(res.data.data.post_list);
                this.setData({
                  topicObj: res.data.data,
                  postList:arr
                })
                WxParse.wxParse('article', 'html', res.data.data.topic_content, that);
              }
            } else {
              wx.showToast({
                title: '该话题已被删除',
                icon: 'none',
                duration: 2000
              })
            }
            refresh == true && wx.stopPullDownRefresh()
          }
        })
      }
    })
  },
  detail: function (e) {
      wx.navigateTo({
        url: '../details/details?id=' + e.currentTarget.dataset.id
      })
  },
  focus:function(e){
    let is_follow = this.data.topicObj.is_follow;
    if (is_follow == 0) {
      this.tofoucs(is_follow);
    } else {
      wx.showModal({
        title: '提示',
        content: '确认取消收藏吗？',
        success: res => {
          if (res.confirm) {

            this.tofoucs(is_follow);
          }
        }
      })
    }
  },
  tofoucs(types){
    wx.request({
      url: base_url + '/Topic/followOrCancel',
      data: {
        'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
        'topic_id': this.data.topic_id,

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: res => {
        wx.hideLoading();
        if (res.data.success == '1') {
          if (types == 0) {
            wx.showToast({
              title: '关注成功',
              icon: 'success',
              duration: 2000
            })
            this.setData({
              'topicObj.is_follow': 1
            })
          } else {
            this.setData({
              'topicObj.is_follow': 0
            })
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }

      }
    })
  },
  handleZan: function (e) {
    let i = e.currentTarget.dataset.id;
    util.zan({
      'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
      'post_id': e.currentTarget.dataset.postid
    }, (res) => {
      let is_zan = 'postList[' + i + '].is_zan';
      let zan_num = 'postList[' + i + '].zan_num';
      let num = Number(this.data.postList[i].zan_num);
      this.setData({
        [is_zan]: this.data.postList[i].is_zan == 0 ? 1 : 0,
        [zan_num]: this.data.postList[i].is_zan == 0 ? num + 1 : num - 1
      })
    })
  },
  commit(){
    wx.navigateTo({
      url: '../post/post?id=' + this.data.topicObj.topic_id
    })
  }
})